package geo.com.geosdkdemo.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import geo.com.geosdk.GeoManage;
import geo.com.geosdk.listener.ConnectCorsServerListener;
import geo.com.geosdk.listener.DeviceCommandSendListener;
import geo.com.geosdk.listener.GetCorsMountPointsListener;
import geo.com.geosdk.listener.GetMountPointsFromHostListener;
import geo.com.geosdk.modelsetting.GeneralBean.CorsParameter;
import geo.com.geosdk.modelsetting.GeneralBean.DataLinkParameter;
import geo.com.geosdk.modelsetting.GeneralBean.DataLinkType;
import geo.com.geosdk.modelsetting.GeneralBean.InstrumentParameter;
import geo.com.geosdk.modelsetting.GeneralBean.InstrumentWorkingMode;
import geo.com.geosdk.modelsetting.GeneralBean.SateSystemEnableParameter;
import geo.com.geosdk.modelsetting.RoverModel.RoverWorkingModeParameter;
import geo.com.geosdkdemo.R;
import geo.com.geosdkdemo.event.DialogDismissEvent;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;


/*
* 1.连接成功的话，先显示主机里面的数据
*   //移动站通用数据
    private RoverWorkingModeParameter roverWorkingModeParameter = GeoManage.getInstance().getRoverWorkingModeParamete();
    //数据链
    private DataLinkParameter dataLinkParameter = GeoManage.getInstance().getDataLinkParameter();
    //卫星系统
    private SateSystemEnableParameter systemEnableParameter=new SateSystemEnableParameter();
*
* 2.设置--通过对RoverWorkingModeParameter、DataLinkParameter、SateSystemEnableParameter三个赋值，从而发送命令
*

* */
public class RoverSetupActivity extends AppCompatActivity implements View.OnClickListener {
    //从主机得到的信息
    private RoverWorkingModeParameter roverWorkingModeParameter = GeoManage.getInstance().getRoverWorkingModeParamete();
    private DataLinkParameter dataLinkParameter = GeoManage.getInstance().getDataLinkParameter();
    private SateSystemEnableParameter systemEnableParameter= GeoManage.getInstance().getSystemEnableParameter();
    private InstrumentParameter mInstrumentParameter= GeoManage.getInstance().getInstrumentParameter();
    public static final String DIALOGNAME = "DIALOGNAME";
    public static final String MOUNTPOINTS = "MOUNTPOINTS";
    private ImageView ivCommonBack;
    private TextView tvCommonTitle;
    private EditText etElevationMask;
    private ImageView ivSwitchPpkOn;
    private ImageView ivSwitchPpkOff;
    private ConstraintLayout clPpk;
    private EditText etRecordPointname;
    private EditText etRecordInterval;
    private RelativeLayout rlDataLink;
    private RelativeLayout rlChannelNumber;
    private TextView tvDataLink;
    private ConstraintLayout clDataLinkNetwork;
    private ConstraintLayout clGga;
    private RelativeLayout rlConnectMode;
    private TextView tvConnectModel;
    private EditText etGga;
    private ImageView ivSwitchConnectNetworkOn;
    private ImageView ivSwitchConnectNetworkOff;
    private EditText etCorsIp;
    private EditText etCorsPort;
    private EditText etCorsUser;
    private EditText etCorsPass;
    private RelativeLayout rlCorsMountPoint;
    private ConstraintLayout clDataLinkBluetooth;
    private Button btnGetMountpoint;
    private Button btnGetMountPointFromHost;
    private RelativeLayout rlCorsProgress;
    private Button btnStartConnect;
    private EditText etCorsMountPoint;
    private Button btnDetermine;
    private FrameLayout flLoading;
    private ImageView ivLoading;
    private TextView tvLoadingTitle;
    private Animation anim;
    private ImageView ivSwitchGpsOn;
    private ImageView ivSwitchGpsOff;
    private ImageView ivSwitchGlonassOn;
    private ImageView ivSwitchGlonassOff;
    private ImageView ivSwitchBeidouOn;
    private ImageView ivSwitchBeidouOff;
    private ImageView ivSwitchGalileoOn;
    private ImageView ivSwitchGalileoOff;
    private ImageView ivSwitchSbasOn;
    private ImageView ivSwitchSbasOff;
    private ImageView ivSwitchQzssOn;
    private ImageView ivSwitchQzssOff;

    private ConstraintLayout clDataLinkUhf;
    private ConstraintLayout clCors;
    private TextView tvChannelNumber;
    private EditText etFrequency;
    private RelativeLayout rlProtocol;
    private TextView tvProtocol;
    private ConstraintLayout clDataLinkExternal;
    private EditText etBaud;
    private String str;
    private ProgressBar progressBar;
    private int mnCountReceiver = 0;;
    private ArrayList<String> mountPoints;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rover_setup);
        initView();
        initData();
    }

    private void initView() {
        ivCommonBack=findViewById(R.id.iv_common_back);
        tvCommonTitle=findViewById(R.id.tv_common_title);
        etElevationMask = (EditText)findViewById( R.id.et_elevation_mask );
        ivSwitchPpkOn = (ImageView)findViewById( R.id.iv_switch_ppk_on );
        ivSwitchPpkOff = (ImageView)findViewById( R.id.iv_switch_ppk_off );
        clPpk = (ConstraintLayout)findViewById( R.id.cl_ppk );
        etRecordPointname = (EditText)findViewById( R.id.et_record_pointname );
        etRecordInterval = (EditText)findViewById( R.id.et_record_interval );
        rlDataLink = (RelativeLayout)findViewById( R.id.rl_data_link );
        tvDataLink = (TextView)findViewById( R.id.tv_data_link);
        clDataLinkNetwork = (ConstraintLayout)findViewById( R.id.cl_data_link_network);
        clGga = (ConstraintLayout)findViewById( R.id.cl_gga );
        rlConnectMode = (RelativeLayout)findViewById( R.id.rl_connect_mode );
        rlChannelNumber = (RelativeLayout)findViewById( R.id.rl_channel_number );
        tvConnectModel = (TextView)findViewById( R.id.tv_connect_model );
        etGga = (EditText)findViewById( R.id.et_gga );
        ivSwitchConnectNetworkOn = (ImageView)findViewById( R.id.iv_switch_connect_network_on );
        ivSwitchConnectNetworkOff = (ImageView)findViewById( R.id.iv_switch_connect_network_off );
        etCorsIp = (EditText)findViewById( R.id.et_cors_ip );
        etCorsPort = (EditText)findViewById( R.id.et_cors_port );
        etCorsUser = (EditText)findViewById( R.id.et_cors_user );
        etCorsPass = (EditText)findViewById( R.id.et_cors_pass );
        rlCorsMountPoint = (RelativeLayout) findViewById( R.id.rl_cors_mount_point );
        clDataLinkBluetooth = (ConstraintLayout) findViewById( R.id.cl_data_link_bluetooth );
        btnGetMountpoint = (Button) findViewById( R.id.btn_get_mountpoint );
        btnGetMountPointFromHost = (Button) findViewById( R.id.btn_get_mountpoint_from_host );
        rlCorsProgress = (RelativeLayout) findViewById( R.id.rl_cors_progress );
        btnStartConnect = (Button) findViewById( R.id.btn_start_connect );
        etCorsMountPoint = (EditText) findViewById( R.id.et_cors_mount_point);
        btnDetermine = (Button)findViewById( R.id.btn_determine );
        flLoading = (FrameLayout)findViewById( R.id.fl_loading );
        ivLoading = (ImageView)findViewById( R.id.iv_loading );
        tvLoadingTitle = (TextView)findViewById( R.id.tv_loading_title );
        ivSwitchGpsOn = (ImageView)findViewById( R.id.iv_switch_gps_on );
        ivSwitchGpsOff = (ImageView)findViewById( R.id.iv_switch_gps_off );
        ivSwitchGlonassOn = (ImageView)findViewById( R.id.iv_switch_glonass_on );
        ivSwitchGlonassOff = (ImageView)findViewById( R.id.iv_switch_glonass_off );
        ivSwitchBeidouOn = (ImageView)findViewById( R.id.iv_switch_beidou_on );
        ivSwitchBeidouOff = (ImageView)findViewById( R.id.iv_switch_beidou_off );
        ivSwitchGalileoOn = (ImageView)findViewById( R.id.iv_switch_galileo_on );
        ivSwitchGalileoOff = (ImageView)findViewById( R.id.iv_switch_galileo_off );
        ivSwitchSbasOn = (ImageView)findViewById( R.id.iv_switch_sbas_on );
        ivSwitchSbasOff = (ImageView)findViewById( R.id.iv_switch_sbas_off );
        ivSwitchQzssOn = (ImageView)findViewById( R.id.iv_switch_qzss_on );
        ivSwitchQzssOff = (ImageView)findViewById( R.id.iv_switch_qzss_off );
        progressBar = (ProgressBar)findViewById( R.id.progressBar_ReceiverData );

        clDataLinkUhf = (ConstraintLayout)findViewById( R.id.cl_data_link_uhf );
        clCors = (ConstraintLayout)findViewById( R.id.cl_cors );
        tvChannelNumber = (TextView)findViewById( R.id.tv_channel_number );
        etFrequency = (EditText)findViewById( R.id.et_frequency );
        rlProtocol = (RelativeLayout)findViewById( R.id.rl_protocol );
        tvProtocol = (TextView)findViewById( R.id.tv_protocol );
        clDataLinkExternal = (ConstraintLayout)findViewById( R.id.cl_data_link_external );
        etBaud = (EditText)findViewById( R.id.et_baud );


        ivCommonBack.setOnClickListener(this);
        ivSwitchPpkOn.setOnClickListener(this);
        ivSwitchPpkOff.setOnClickListener(this);
        rlDataLink.setOnClickListener(this);
        rlConnectMode.setOnClickListener(this);
        ivSwitchConnectNetworkOn.setOnClickListener(this);
        ivSwitchConnectNetworkOff.setOnClickListener(this);
        ivSwitchGpsOn.setOnClickListener(this);
        ivSwitchGpsOff.setOnClickListener(this);
        ivSwitchGlonassOn.setOnClickListener(this);
        ivSwitchGlonassOff.setOnClickListener(this);
        ivSwitchBeidouOn.setOnClickListener(this);
        ivSwitchBeidouOff.setOnClickListener(this);
        ivSwitchGalileoOff.setOnClickListener(this);
        ivSwitchGalileoOn.setOnClickListener(this);
        ivSwitchSbasOff.setOnClickListener(this);
        ivSwitchSbasOn.setOnClickListener(this);
        ivSwitchQzssOn.setOnClickListener(this);
        ivSwitchQzssOff.setOnClickListener(this);
        rlProtocol.setOnClickListener(this);
        rlChannelNumber.setOnClickListener(this);
        btnDetermine.setOnClickListener(this);
        rlCorsMountPoint.setOnClickListener(this);
        btnGetMountpoint.setOnClickListener(this);
        btnGetMountPointFromHost.setOnClickListener(this);
        btnStartConnect.setOnClickListener(this);

    }

    private void initData() {
        tvCommonTitle.setText("移动站设置");
        mountPoints=new ArrayList<>();
        if (!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }
        progressBar.setMax(5000);
        progressBar.setProgress(0);

        //进来显示接收机解析的数据
        showRTKData();
    }

    private void showRTKData() {
        //显示通用
        etElevationMask.setText(String.valueOf(roverWorkingModeParameter.mElevationMask));
        boolean mUsePPK = roverWorkingModeParameter.mUsePPK;
        if(mUsePPK){
            onClickPPkOff();
            etRecordPointname.setText(roverWorkingModeParameter.ppkParameter.mRecordPointname);
            etRecordInterval.setText(String.valueOf(roverWorkingModeParameter.ppkParameter.mRecordInterval));
        }else{
            onClickPPkOn();
        }
        //显示数据链
        String typeTitle = dataLinkParameter.mDataLinkType.getTypeTitle(dataLinkParameter.mDataLinkType);
        showDataLinkData(typeTitle);

        //显示卫星系统
        showSatelliteSystem();

    }

    private void showDataLinkData(String datalink) {
        btnGetMountPointFromHost.setVisibility(GONE);
        clDataLinkBluetooth.setVisibility(GONE);
        clGga.setVisibility(View.VISIBLE);
        rlConnectMode.setClickable(true);

        tvDataLink.setText(datalink);
        switch (datalink){
            case "主机网络":
                showDatalinkNetwork();
                break;
            case "内置电台":
                showDatalinkUHF();
                break;
            case "外置电台":
                showDatalinkExt();
                break;
            case "手簿网络":
                showBlueTooth();
                break;
            default:
                showDatalinkOther();
                break;
        }
    }

    private void showBlueTooth() {
        //默认只显示NTRIP
        tvConnectModel.setText("NTRIP");
        rlConnectMode.setClickable(false);
        clGga.setVisibility(GONE);
        clDataLinkNetwork.setVisibility(View.VISIBLE);
        clDataLinkBluetooth.setVisibility(VISIBLE);
        clDataLinkUhf.setVisibility(GONE);
        clDataLinkExternal.setVisibility(GONE);


        //判断cors连接
        if(GeoManage.getInstance().isConnectedCors()){
            btnStartConnect.setText(R.string.geo_stop_cors);
            btnGetMountpoint.setEnabled(false);
        }
        //cors连接回调
        GeoManage.getInstance().setConnectCorsServerListener(new ConnectCorsServerListener(){
            @Override
            public void OnConnectCorsServerStatusCallBack(final boolean isConnectSucc, final String message) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showToast(message);
                        //更新UI
                        if(isConnectSucc){
                            btnStartConnect.setText(R.string.geo_stop_cors);
                            btnGetMountpoint.setEnabled(false);
                        }else{
                            btnGetMountpoint.setEnabled(true);
                            btnStartConnect.setText(R.string.geo_start_cors);
                        }
                    }
                });
            }
            //从cors接收差分数据成功,更新接收进度条
            @Override
            public void ConnectCorsServerProgressCallBack(int progress) {
                mnCountReceiver += progress;
                mnCountReceiver = (mnCountReceiver%5000);
                progressBar.setProgress(mnCountReceiver);
            }
        });
        CorsParameter corsConfig = GeoManage.getInstance().getCorsConfig();
        boolean autoConnectCors = corsConfig.isAutoConnectCors();
        String strIP = corsConfig.getStrIP();
        int port = corsConfig.getPort();
        String strUser = corsConfig.getStrUser();
        String strPassword = corsConfig.getStrPassword();
        String strMountPoint = corsConfig.getStrMountPoint();
        if(autoConnectCors){
            onClickAutoConnectNetOff();
        }else{
            onClickAutoConnectNetOn();
        }
        etCorsIp.setText(strIP);
        etCorsPort.setText(String.valueOf(port));
        etCorsUser.setText(strUser);
        etCorsPass.setText(strPassword);
        etCorsMountPoint.setText(strMountPoint);
    }

    private void showDatalinkOther() {
        clDataLinkExternal.setVisibility(GONE);
        clDataLinkUhf.setVisibility(GONE);
        clDataLinkNetwork.setVisibility(GONE);
    }

    private void showDatalinkExt() {
        clDataLinkExternal.setVisibility(View.VISIBLE);
        clDataLinkUhf.setVisibility(GONE);
        clDataLinkNetwork.setVisibility(GONE);

        etBaud.setText(String.valueOf(dataLinkParameter.mExternal.Baud));
    }

    private void showDatalinkUHF() {
        clDataLinkUhf.setVisibility(View.VISIBLE);
        clDataLinkNetwork.setVisibility(GONE);
        clDataLinkExternal.setVisibility(GONE);

        int channelNum=dataLinkParameter.mUHF.mChannelNumber;
        tvChannelNumber.setText(String.valueOf(channelNum));
        if(channelNum==8){
            etFrequency.setEnabled(true);
        }else{
            etFrequency.setEnabled(false);
        }
        //每一个通道对应一个频率，通道1对应mFrequencyofChannel[0]
        etFrequency.setText(String.valueOf(dataLinkParameter.mUHF.mFrequencyofChannel[channelNum-1]));
        tvProtocol.setText(dataLinkParameter.mUHF.mUhfProtocolType.getTypeTitle(dataLinkParameter.mUHF.mUhfProtocolType));
    }

    //显示网络数据链
    private void showDatalinkNetwork() {
        clDataLinkNetwork.setVisibility(View.VISIBLE);
        clDataLinkUhf.setVisibility(GONE);
        clDataLinkExternal.setVisibility(GONE);
        String connecModel=dataLinkParameter.mNetwork.mConnectionMode.getTypeTitle(dataLinkParameter.mNetwork.mConnectionMode);
        tvConnectModel.setText(connecModel);
        etGga.setText(String.valueOf(dataLinkParameter.mNetwork.mUploadGGAInterval));
        if(dataLinkParameter.mNetwork.mAutoConnect){
            onClickAutoConnectNetOff();
        }else{
            onClickAutoConnectNetOn();
        }

        etCorsIp.setText(dataLinkParameter.mNetwork.mCorsParameter.mStrIP);
        etCorsPort.setText(String.valueOf(dataLinkParameter.mNetwork.mCorsParameter.mPort));

        if(connecModel.equals("TCP客户端")){//此时不需要显示CORS用户名密码，接入点
            clCors.setVisibility(GONE);
            btnGetMountPointFromHost.setVisibility(GONE);
            return;
        }else{
            btnGetMountPointFromHost.setVisibility(VISIBLE);
        }

        etCorsUser.setText(dataLinkParameter.mNetwork.mCorsParameter.mStrUser);
        etCorsPass.setText(dataLinkParameter.mNetwork.mCorsParameter.mStrPassword);
        etCorsMountPoint.setText(dataLinkParameter.mNetwork.mCorsParameter.mStrMountPoint);
    }

    private void showSatelliteSystem() {
        if(systemEnableParameter.UseGPS){
            onClickGpsOff();
        }else{
            onClickGpsOn();
        }

        if(systemEnableParameter.UseGlonass){
            onClickGlonassOff();
        }else{
            onClickGlonassOn();
        }

        if(systemEnableParameter.UseBeiDou){
            onClickBeidouOff();
        }else{
            onClickBeidouOn();
        }

        if(systemEnableParameter.UseGalileo){
            onClickGalileoOff();
        }else{
            onClickGalileoOn();
        }

        if(systemEnableParameter.UseSBAS){
            onClickSbasOff();
        }else{
            onClickSbasOn();
        }

        if(systemEnableParameter.UseQZSS){
            onClickQzssOff();
        }else{
            onClickQzssOn();
        }

        if(systemEnableParameter.UseGPS){
            onClickGpsOff();
        }else{
            onClickGpsOn();
        }


    }


    private void onClickBtnDetermine() {
        //开始设置命令
        //1.开始动画
        startAnim();
        //2.监听发送状态
        GeoManage.getInstance().setDeviceCommandSendListener(new DeviceCommandSendListener() {
            @Override
            public void OnDeviceCommandSendCallBack(final int status, final String strFunction) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(status==0){
                            str="开始"+strFunction;
                        }else if(status==1){
                            str=strFunction+"完成";
                        }else if(status==2){
                            str=strFunction+"超时";
                            //超时这边的操作是继续发送下一条命令
                        }else if(status==3){//全部发送成功
                            str="全部命令发送完成";
                            stopAnim();
                            finish();
                        }
                        tvLoadingTitle.setText(str);
                        Log.d("OnDeviceCommandSend", "OnDeviceCommandSendCallBack: "+str);
                    }
                });

            }
        });

        //设置参数
        //1.设置移动站通用选项
        RoverWorkingModeParameter roverWorkingModeParameter=new RoverWorkingModeParameter();
        roverWorkingModeParameter.mElevationMask= Integer.parseInt(etElevationMask.getText().toString().trim());
        if(ivSwitchPpkOn.getVisibility()== View.VISIBLE){//启用PPK
            roverWorkingModeParameter.mUsePPK=true;
            roverWorkingModeParameter.ppkParameter.mRecordPointname=etRecordPointname.getText().toString().trim();
            roverWorkingModeParameter.ppkParameter.mRecordInterval= Integer.parseInt(etRecordInterval.getText().toString().trim());
        }else{
            roverWorkingModeParameter.mUsePPK=false;
        }

        //2.初始化DataLinkParameter，设置数据链模式
        DataLinkParameter dataLinkParameter=new DataLinkParameter();
        String datalink=tvDataLink.getText().toString().trim();
        dataLinkParameter.mDataLinkType=dataLinkParameter.mDataLinkType.titleToEnum(datalink);
        //先默认设置不使用中国精度，只有数据链是中国精度时才置为true
        dataLinkParameter.isUseLBand=false;
        switch (datalink){
            case "主机网络":
                setDataLinkNet(dataLinkParameter);
                break;
            case "内置电台":
                setDataLinkUHF(dataLinkParameter);
                break;
            case "外置电台":
                setDataLinkExt(dataLinkParameter);
                break;
            case "中国精度":
                setDataLinkLBand(dataLinkParameter);
                break;

        }

        //3.初始化SateSystemEnableParameter，设置是否使用6大卫星参数
        SateSystemEnableParameter sateSystemEnableParameter=new SateSystemEnableParameter();
        sateSystemEnableParameter.UseGPS = ivSwitchGpsOn.getVisibility() == View.VISIBLE;
        sateSystemEnableParameter.UseGlonass = ivSwitchGlonassOn.getVisibility() == View.VISIBLE;
        sateSystemEnableParameter.UseBeiDou = ivSwitchBeidouOn.getVisibility() == View.VISIBLE;
        sateSystemEnableParameter.UseGalileo=ivSwitchGalileoOn.getVisibility() == View.VISIBLE;
        sateSystemEnableParameter.UseQZSS=ivSwitchQzssOn.getVisibility() == View.VISIBLE;
        sateSystemEnableParameter.UseSBAS=ivSwitchSbasOn.getVisibility() == View.VISIBLE;

        //4.调用接口进行命令发送
        //sateSystemEnableParameter传递NUll表示不设置相关命令
        GeoManage.getInstance().setRoverModel(roverWorkingModeParameter,dataLinkParameter,sateSystemEnableParameter);

    }

    private void setDataLinkLBand(DataLinkParameter dataLinkParameter) {
        dataLinkParameter.isUseLBand=true;
    }

    private void setDataLinkExt(DataLinkParameter dataLinkParameter) {
        dataLinkParameter.mExternal.Baud= Integer.parseInt(etBaud.getText().toString().trim());
    }

    private void setDataLinkUHF(DataLinkParameter dataLinkParameter) {
        dataLinkParameter.mUHF.mChannelNumber= Integer.parseInt(tvChannelNumber.getText().toString().trim());
        //8通道为自定义通道,给mFrequencyofChannel[7]赋予自己写的值
        if(dataLinkParameter.mUHF.mChannelNumber==8){
            dataLinkParameter.mUHF.mFrequencyofChannel[7]= Double.parseDouble(etFrequency.getText().toString().trim());
        }
        dataLinkParameter.mUHF.mUhfProtocolType= dataLinkParameter.mUHF.mUhfProtocolType.titleToEnum(tvProtocol.getText().toString().trim());

    }

    private void setDataLinkNet(DataLinkParameter dataLinkParameter) {
        dataLinkParameter.mNetwork.mConnectionMode= dataLinkParameter.mNetwork.mConnectionMode.titleToEnum(tvConnectModel.getText().toString().trim());
        dataLinkParameter.mNetwork.mUploadGGAInterval= Integer.parseInt(etGga.getText().toString().trim());
        dataLinkParameter.mNetwork.mAutoConnect = ivSwitchConnectNetworkOn.getVisibility() == View.VISIBLE;
        dataLinkParameter.mNetwork.mCorsParameter.mStrIP=etCorsIp.getText().toString().trim();
        dataLinkParameter.mNetwork.mCorsParameter.mPort= Integer.parseInt(etCorsPort.getText().toString().trim());
        if(tvConnectModel.getText().toString().trim().equals("TCP客户端"))
            return;
        dataLinkParameter.mNetwork.mCorsParameter.mStrUser=etCorsUser.getText().toString().trim();
        dataLinkParameter.mNetwork.mCorsParameter.mStrPassword=etCorsPass.getText().toString().trim();
        dataLinkParameter.mNetwork.mCorsParameter.mStrMountPoint= etCorsMountPoint.getText().toString().trim();
    }

    //选择对话框里面的选项后做的操作
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void showChannelNum(DialogDismissEvent event) {
        String title=event.getTitle();
        String data=event.getData();
        switch (title) {
            case "数据链":
                showDataLinkData(data);
                break;
            case "连接模式":
                tvConnectModel.setText(data);
                clCors.setVisibility(View.VISIBLE);
                if (data.equals("TCP客户端")) {//此时不需要显示CORS用户名密码，接入点
                    clCors.setVisibility(GONE);
                    btnGetMountPointFromHost.setVisibility(GONE);
                }else {
                    btnGetMountPointFromHost.setVisibility(VISIBLE);
                }
                break;
            case "通道":
                tvChannelNumber.setText(data);
                //只有8通道允许自定义，其他都是默认不允许修改
                int channelNum = Integer.parseInt(data);
                if (channelNum == 8) {
                    etFrequency.setEnabled(true);
                } else {
                    etFrequency.setEnabled(false);
                }
                //每一个通道对应一个频率，通道1对应mFrequencyofChannel[0]
                etFrequency.setText(String.valueOf(dataLinkParameter.mUHF.mFrequencyofChannel[channelNum - 1]));
                break;
            case "协议":
                tvProtocol.setText(data);
                break;
            case "接入点":
                etCorsMountPoint.setText(data);
                break;
        }
    }

    private void onClickQzssOn() {
        ivSwitchQzssOn.setVisibility(GONE);
        ivSwitchQzssOff.setVisibility(View.VISIBLE);
    }

    private void onClickQzssOff() {
        ivSwitchQzssOff.setVisibility(GONE);
        ivSwitchQzssOn.setVisibility(View.VISIBLE);
    }

    private void onClickSbasOn() {
        ivSwitchSbasOn.setVisibility(GONE);
        ivSwitchSbasOff.setVisibility(View.VISIBLE);
    }

    private void onClickSbasOff() {
        ivSwitchSbasOff.setVisibility(GONE);
        ivSwitchSbasOn.setVisibility(View.VISIBLE);
    }

    private void onClickGalileoOn() {
        ivSwitchGalileoOn.setVisibility(GONE);
        ivSwitchGalileoOff.setVisibility(View.VISIBLE);
    }

    private void onClickGalileoOff() {
        ivSwitchGalileoOff.setVisibility(GONE);
        ivSwitchGalileoOn.setVisibility(View.VISIBLE);
    }

    private void onClickBeidouOn() {
        ivSwitchBeidouOn.setVisibility(GONE);
        ivSwitchBeidouOff.setVisibility(View.VISIBLE);
    }

    private void onClickBeidouOff() {
        ivSwitchBeidouOff.setVisibility(GONE);
        ivSwitchBeidouOn.setVisibility(View.VISIBLE);
    }

    private void onClickGlonassOn() {
        ivSwitchGlonassOn.setVisibility(GONE);
        ivSwitchGlonassOff.setVisibility(View.VISIBLE);
    }

    private void onClickGlonassOff() {
        ivSwitchGlonassOff.setVisibility(GONE);
        ivSwitchGlonassOn.setVisibility(View.VISIBLE);
    }

    private void onClickGpsOn() {
        ivSwitchGpsOn.setVisibility(GONE);
        ivSwitchGpsOff.setVisibility(View.VISIBLE);
    }

    private void onClickGpsOff() {
        ivSwitchGpsOff.setVisibility(GONE);
        ivSwitchGpsOn.setVisibility(View.VISIBLE);
    }

    private void onClickAutoConnectNetOn() {
        ivSwitchConnectNetworkOn.setVisibility(GONE);
        ivSwitchConnectNetworkOff.setVisibility(View.VISIBLE);
        //不自动重连
        GeoManage.getInstance().setAutoConnectCors(false);
    }

    private void onClickAutoConnectNetOff() {
        ivSwitchConnectNetworkOff.setVisibility(GONE);
        ivSwitchConnectNetworkOn.setVisibility(View.VISIBLE);
        //自动重连
        GeoManage.getInstance().setAutoConnectCors(true);
    }

    private void onClickChannelNumber() {
        Intent intent=new Intent(this, SelectDialogActivity.class);
        intent.putExtra(DIALOGNAME,"通道");
        startActivity(intent);
    }

    private void onClickUhfProtocol() {
        Intent intent=new Intent(this, SelectDialogActivity.class);
        intent.putExtra(DIALOGNAME,"协议");
        startActivity(intent);
    }


    private void onClickConnectMode() {
        Intent intent=new Intent(this, SelectDialogActivity.class);
        intent.putExtra(DIALOGNAME,"连接模式");
        startActivity(intent);
    }

    private void onClickRlDataLink() {
        Intent intent=new Intent(this, SelectDialogActivity.class);
        intent.putExtra(DIALOGNAME,"数据链");
        startActivity(intent);
    }

    private void onClickPPkOn() {
        ivSwitchPpkOn.setVisibility(GONE);
        ivSwitchPpkOff.setVisibility(View.VISIBLE);
        clPpk.setVisibility(GONE);
    }

    private void onClickPPkOff() {
        ivSwitchPpkOff.setVisibility(GONE);
        ivSwitchPpkOn.setVisibility(View.VISIBLE);
        clPpk.setVisibility(View.VISIBLE);
    }


    @Override
    public void onClick(View v) {
        if(v==ivCommonBack){
            finish();
        }else if(v==ivSwitchPpkOff){
            onClickPPkOff();
        }else if(v==ivSwitchPpkOn){
            onClickPPkOn();
        }else if(v==rlDataLink){
            onClickRlDataLink();
        }else if(v==rlConnectMode){
            onClickConnectMode();
        }else if(v==rlChannelNumber){
            onClickChannelNumber();
        }else if(v==rlProtocol){
            onClickUhfProtocol();
        }else if(v==rlCorsMountPoint){
            onClickMountPoint();
        }else if(v==btnGetMountpoint){
            onClickGetMountPoint();
        }else if(v==btnGetMountPointFromHost){
            onClickGetMountPointFromHost();
        }else if(v==btnStartConnect){
            onClickStartConnect();
        }else if(v==btnDetermine){
            onClickBtnDetermine();
        }else if(v==ivSwitchConnectNetworkOff){
            onClickAutoConnectNetOff();
        }else if(v==ivSwitchConnectNetworkOn){
            onClickAutoConnectNetOn();
        }else if(v==ivSwitchGpsOff){
            onClickGpsOff();
        }else if(v==ivSwitchGpsOn){
            onClickGpsOn();
        }else if(v==ivSwitchGlonassOff){
            onClickGlonassOff();
        }else if(v==ivSwitchGlonassOn){
            onClickGlonassOn();
        }else if(v==ivSwitchBeidouOff){
            onClickBeidouOff();
        }else if(v==ivSwitchBeidouOn){
            onClickBeidouOn();
        }else if(v==ivSwitchGalileoOff){
            onClickGalileoOff();
        }else if(v==ivSwitchGalileoOn){
            onClickGalileoOn();
        }else if(v==ivSwitchSbasOff){
            onClickSbasOff();
        }else if(v==ivSwitchSbasOn){
            onClickSbasOn();
        }else if(v==ivSwitchQzssOff){
            onClickQzssOff();
        }else if(v==ivSwitchQzssOn){
            onClickQzssOn();
        }
    }

    private void onClickGetMountPointFromHost() {
        String corsIp=etCorsIp.getText().toString().trim();
        int corsPort= Integer.parseInt(etCorsPort.getText().toString().trim());
        String corsUser=etCorsUser.getText().toString().trim();
        String corsPass=etCorsPass.getText().toString().trim();

        //开始设置命令
        //1.开始动画
        startAnim();
        //2.监听发送状态
        GeoManage.getInstance().setDeviceCommandSendListener(new DeviceCommandSendListener() {
            @Override
            public void OnDeviceCommandSendCallBack(final int status, final String strFunction) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(status==0){
                            str="开始"+strFunction;
                        }else if(status==1){
                            str=strFunction+"完成";
                        }else if(status==2){
                            str=strFunction+"超时";
                            //超时这边的操作是继续发送下一条命令
                        }else if(status==3){//全部发送成功
                            str="全部命令发送完成";
                            stopAnim();
                        }
                        tvLoadingTitle.setText(str);

                        //接入点回调
                        GeoManage.getInstance().setGetMountPointsFromHostListener(new GetMountPointsFromHostListener() {
                            @Override
                            public void OnGetMountPointsFromHostStatusCallBack(final boolean bSucceed, final ArrayList<String> mountPoint) {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if(bSucceed){
                                            showToast("获取接入点成功！");
                                            //传递获得的数据到对话框
                                            mountPoints=mountPoint;
                                        }else{
                                            showToast("获取接入点失败！");
                                        }
                                    }
                                });
                            }
                        });
                    }
                });

            }
        });

        GeoManage.getInstance().getCorsMountPointsFromHostNetwork(corsIp,corsPort,corsUser,corsPass);

    }


    private void onClickStartConnect() {
        //只是针对移动站
        if(GeoManage.getInstance().getInstrumentParameter().mode!= InstrumentWorkingMode.Rover){
            showToast("请先设置当前数据链为移动站模式");
            return;
        }
        //只是针对手蒲网络，
        if(dataLinkParameter.mDataLinkType!= DataLinkType.ExtendSource){
            showToast("请先设置当前数据链为手簿网络");
            return;
        }

        if(btnStartConnect.getText().equals(getString(R.string.geo_stop_cors))){//断开连接
            if(GeoManage.getInstance().isConnectedCors()){
                GeoManage.getInstance().setAutoConnectCors(false);//此时是手动断开不需要自动重连
                GeoManage.getInstance().disConnectCors();
                return;
            }
        }

        String corsIp=etCorsIp.getText().toString().trim();
        int corsPort= Integer.parseInt(etCorsPort.getText().toString().trim());
        String corsUser=etCorsUser.getText().toString().trim();
        String corsPass=etCorsPass.getText().toString().trim();
        String mountPoint= etCorsMountPoint.getText().toString().trim();

        //连接cors服务器
        GeoManage.getInstance().connectCorsServer(corsIp,corsPort,corsUser,corsPass,mountPoint);
    }

    private void onClickGetMountPoint() {
        // 手机网络获取挂载点的情况;
        String corsIp=etCorsIp.getText().toString().trim();
        int corsPort= Integer.parseInt(etCorsPort.getText().toString().trim());
        String corsUser=etCorsUser.getText().toString().trim();
        String corsPass=etCorsPass.getText().toString().trim();
        //获取接入点
        GeoManage.getInstance().getCorsMountPoints(corsIp,corsPort,corsUser,corsPass);
        //接入点回调
        GeoManage.getInstance().setGetCorsMountPointsListener(new GetCorsMountPointsListener() {
            @Override
            public void OnGetCorsMountPointsStatusCallBack(final boolean bSucceed, final ArrayList<String> mountPoint) {
                //子线程中
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(bSucceed){
                            showToast("获取接入点完毕！");
                            //传递获得的数据到对话框
                            mountPoints=mountPoint;
                        }else{
                            showToast("接入点获取失败！");
                        }
                    }
                });

            }
        });

    }



    private void onClickMountPoint() {
        Intent intent=new Intent(this, SelectDialogActivity.class);
        intent.putExtra(DIALOGNAME,"接入点");
        intent.putStringArrayListExtra(MOUNTPOINTS,mountPoints);
        startActivity(intent);
    }

    //开始动画
    private void startAnim() {
        anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim_round_rotate);
        LinearInterpolator interpolator = new LinearInterpolator();  //设置匀速旋转，在xml文件中设置会出现卡顿
        anim.setInterpolator(interpolator);
        if (anim != null) {
            flLoading.setVisibility(VISIBLE);
            ivLoading.startAnimation(anim);  //开始动画
        }
    }

    //停止动画
    private void stopAnim() {
        if (anim != null) {
            anim =null;
            flLoading.setVisibility(GONE);
            ivLoading.clearAnimation();
        }
    }

    public void showToast(String str) {
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        if (EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);
        }
        stopAnim();
        super.onDestroy();
    }


}
